package cn.fl.mcr.service.impl;

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.mcr.service.McrGraphicReportService;
import cn.fl.mcr.vo.GraphicReportVO;

/**
 * 
 * @ClassName: McrGraphicReportServiceImpl
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: HZM
 * @date: 2020年3月3日 下午3:01:01
 *
 */
@Service
public class McrGraphicReportServiceImpl implements McrGraphicReportService {

	private static final Logger logger = LoggerFactory.getLogger(McrGraphicReportService.class);
	/**
	 * 
	 * <p>
	 * Title: createColumnarTools
	 * </p>
	 * <p>
	 * Description: 生成 资方数据统计 折线图
	 * </p>
	 * 
	 * @return
	 * @see cn.fl.mcr.service.McrGraphicReportService#createColumnarTools()
	 */
	@Override
	public JFreeChart createColumnarTools(String accountNames,List<GraphicReportVO> graphicReportList ) {
		// 获得数据
		CategoryDataset dataset = getDataSet(graphicReportList);
		// 生成折线图
		JFreeChart chart = this.createFoldLine(accountNames,dataset);
		return chart;
	}

	/**
	 * 
	 * @Title: getDataSet @Description: 获取数据 @param: @return @return:
	 * CategoryDataset @throws
	 */
	private CategoryDataset getDataSet(List<GraphicReportVO> graphicReportList) {
		// 数据可以从数据库中得到
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		
		if(CollectionUtils.isNotEmpty(graphicReportList)) {
			for(GraphicReportVO graphicReport:graphicReportList) {
				dataset.addValue(graphicReport.getLoanAmountBlance(), "放款金额(万元)",DateUtils.formatDateToString(graphicReport.getCrtTm(), DateUtils.MM_DD));
				dataset.addValue(graphicReport.getReturnAmountBlance(), "回款金额(万元)", DateUtils.formatDateToString(graphicReport.getCrtTm(), DateUtils.MM_DD));
			}
		}
		return dataset;
	}

	/**
	 * 
	 * @Title: createCoColumnar 
	 * @Description: TODO(这里用一句话描述这个方法的作用) 
	 * @param: 
	 * @param dataset 
	 * @param: 
	 * @return @return: JFreeChart @throws
	 */
	public JFreeChart createFoldLine(String accountNames,CategoryDataset dataset) {
		JFreeChart chart = ChartFactory.createLineChart(accountNames, "日期", "金额(万元)", dataset, PlotOrientation.VERTICAL, true,	true, true);
		// 数据的行数 即折线的条数
		int datasetSize = dataset.getRowCount();
		BaseLogUtils.info(logger, "createFoldLine", "资方数据统计行数datasetSize:"+datasetSize, CurrentThreadContext.getCurrentUserName());
		getChartByFont(chart, datasetSize);

		return chart;
	}

	private static void getChartByFont(JFreeChart chart, int datasetSize) {
		//设置整个图片的背景色 
		chart.setBackgroundPaint(Color.GRAY); 
		// 设置图片有边框  
        chart.setBorderVisible(true); 
		// 处理主标题的文字
		chart.getTitle().setFont(new Font("宋体", Font.BOLD, 18));
		chart.getTitle().setPaint(Color.WHITE);
		// 处理子标题文字
		chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 15));
		
		// 获取图表区域
		CategoryPlot categoryPlot = (CategoryPlot) chart.getPlot();
		
		categoryPlot.setBackgroundAlpha(1f);
		// 设置前景色透明度（0~1）
		categoryPlot.setForegroundAlpha(1f);
		
		categoryPlot.setBackgroundPaint(Color.BLACK); 
		// 获取X轴
		CategoryAxis categoryAxis = (CategoryAxis) categoryPlot.getDomainAxis();
		// 获取Y轴
		NumberAxis numberAxis = (NumberAxis) categoryPlot.getRangeAxis();
		// 处理X轴上的文字
		categoryAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 12));
		categoryAxis.setTickLabelPaint(Color.WHITE);
		// 处理X轴外的文字
		categoryAxis.setLabelFont(new Font("宋体", Font.BOLD, 12));
		categoryAxis.setLabelPaint(Color.WHITE);
		// 处理Y轴上的文字
		numberAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 15));
		numberAxis.setTickLabelPaint(Color.WHITE);
		// 处理Y轴外的文字
		numberAxis.setLabelFont(new Font("宋体", Font.BOLD, 15));
		numberAxis.setLabelPaint(Color.WHITE);
		
		// 处理Y轴上显示的刻度，以5000作为1格
		numberAxis.setAutoTickUnitSelection(false);
		NumberTickUnit unit = new NumberTickUnit(50);
		numberAxis.setTickUnit(unit);
		// 获取绘图区域
		LineAndShapeRenderer lineAndShapeRenderer = (LineAndShapeRenderer) categoryPlot.getRenderer();
		// 在图形上显示数字
		lineAndShapeRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		lineAndShapeRenderer.setBaseItemLabelsVisible(true);
		lineAndShapeRenderer.setBaseItemLabelFont(new Font("宋体", Font.BOLD, 10));
		
		// 在图形上添加转折点（小矩形）
		Rectangle shape = new Rectangle(5, 5);
		for (int i = 0; i < datasetSize; i++) {
			lineAndShapeRenderer.setSeriesShape(i, shape);
			lineAndShapeRenderer.setSeriesShapesVisible(i, true);
			lineAndShapeRenderer.setBaseItemLabelPaint(Color.WHITE);
		}
	}
}
